package com.duobaoyu.dby.supply.biz.client.resource.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.constant.ServiceNameConstants;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.validator.ValidationList;
import com.duobaoyu.dby.resource.admin.feign.RemoteResourceStateInfoAdminService;
import com.duobaoyu.dby.resource.common.admin.dto.config.StateInfoDTO;
import com.duobaoyu.dby.resource.common.admin.to.config.MappingBatchTO;
import com.duobaoyu.dby.resource.common.admin.to.config.StateInfoTO;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceStateInfoAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.StateBatchDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.StateDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.MappingBatchVO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.MappingVO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.StateInfoVO;
import com.duobaoyu.dby.supply.biz.client.resource.converter.ResourceConverter;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ResourceStateInfoAdminClientImpl
 * @createTime 15:12:00 2023/10/08
 */
@Component
@RequiredArgsConstructor
public class ResourceStateInfoAdminClientImpl implements ResourceStateInfoAdminClient {

    private final ResourceConverter resourceConverter;
    private final RemoteResourceStateInfoAdminService remoteResourceStateInfoAdminService;

    @Override
    public List<MappingBatchVO> stateCodeList(ValidationList<StateBatchDTO> stateBatchs) {
        R<List<MappingBatchTO>> result = remoteResourceStateInfoAdminService.stateCodeList(resourceConverter.toStateBatchList(stateBatchs), SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateBatchs);
        return resourceConverter.toMappingBatchList(result.getData());
    }

    @Override
    public boolean addStateInfo(StateDTO state) {
        StateInfoDTO dto = resourceConverter.toStateInfo(state);
        R<Boolean> result = remoteResourceStateInfoAdminService.add(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, state);
        return result.getData();
    }

    @Override
    public boolean updateStateInfo(StateDTO state) {
        StateInfoDTO dto = resourceConverter.toStateInfo(state);
        R<Boolean> result = remoteResourceStateInfoAdminService.update(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, state);
        return result.getData();
    }

    @Override
    public boolean deleteStateInfo(Long stateInfoId) {
        R<Boolean> result = remoteResourceStateInfoAdminService.delete(stateInfoId, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateInfoId);
        return result.getData();
    }

    @Override
    public StateInfoVO queryStateInfoDetail(Long stateInfoId) {
        R<StateInfoTO> result = remoteResourceStateInfoAdminService.queryOne(stateInfoId, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateInfoId);
        return resourceConverter.toStateInfo(result.getData());
    }

    @Override
    public Page<StateInfoVO> queryStateInfoPage(StateDTO state) {
        StateInfoDTO dto = resourceConverter.toStateInfo(state);
        R<Page<StateInfoTO>> result = remoteResourceStateInfoAdminService.page(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, state);
        return resourceConverter.toStateInfoPage(result.getData());
    }

    @Override
    @Deprecated
    public List<MappingVO> getStateInfoToLocalMappingCode(String stateCode, String label) {
        ValidationList stateBatchs = new ValidationList();
        StateBatchDTO stateBatch = new StateBatchDTO();
        stateBatch.setStateCode(stateCode);
        stateBatch.setLabel(label);
        stateBatch.setIfCode(Boolean.TRUE);
        stateBatchs.add(stateBatch);
        R<List<MappingBatchTO>> result = remoteResourceStateInfoAdminService.stateCodeList(resourceConverter.toStateBatchList(stateBatchs), SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, stateBatchs);
        List<MappingBatchTO> data = result.getData();
        if(CollectionUtils.isEmpty(data)) {
            return Collections.emptyList();
        }
        return data.stream().map(MappingBatchTO::getMappings).flatMap(Collection::stream).
                map(item->resourceConverter.toMapping(item)).collect(Collectors.toList());
    }
}
